#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Project: devops
#  Author: an00
#  Create: 2018/6/23 21:47

"""
IDC app serializer
"""

from rest_framework import serializers

from idcs.models import Idc


class IdcSerializer(serializers.Serializer):
    """
    IDC 序列化类
    """
    id = serializers.IntegerField(read_only=True, help_text='只读，    不可修改')
    name = serializers.CharField(required=True, max_length=32, label='机房名称', help_text='机房名称',
                                 error_messages={'blank': '不能为空', 'required': '字段必须填写'})
    address = serializers.CharField(required=True, max_length=128, label='机房地址', help_text='机房地址')
    manage = serializers.CharField(required=True, max_length=32, label='机房联系人', help_text='机房联系人')
    phone = serializers.CharField(required=True, max_length=11, label='联系电话', help_text='联系电话')
    email = serializers.EmailField(required=True, label='联系邮箱', help_text='联系邮箱')
    letter = serializers.CharField(required=True, max_length=12, label='机房简称', help_text='机房简称')
    ctime = serializers.DateTimeField(read_only=True, label='创建时间', help_text='创建时间，自动添加，只读')

    def create(self, validated_data):
        return Idc.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name", instance.name)
        instance.address = validated_data.get("address", instance.address)
        instance.phone = validated_data.get("phone", instance.phone)
        instance.email = validated_data.get("email", instance.email)
        instance.manage = validated_data.get("manage", instance.manage)
        instance.letter = validated_data.get("letter", instance.letter)
        instance.save()
        return instance
